import { HighlightLayer, ActionManager, ExecuteCodeAction, Color3 } from '@babylonjs/core'

/**
 * @function mesh绑定事件
 * @param scene
 * @param mesh
 * @param fn
 */
function meshBindAction(scene, mesh, fn = () => {}) {
  scene.blockfreeActiveMeshesAndRenderingGroups = true

  // 高亮
  const hl = new HighlightLayer('hl1', scene)
  // 注册mesh事件
  mesh.actionManager = new ActionManager(scene)

  // 点击事件
  mesh.actionManager.registerAction(new ExecuteCodeAction(ActionManager.OnPickTrigger, () => { return fn() }))

  // pointer hover
  mesh.actionManager.registerAction(new ExecuteCodeAction(ActionManager.OnPointerOverTrigger, () => {
    hl.addMesh(mesh, Color3.Green())
  }))

  // pointer hover out
  mesh.actionManager.registerAction(new ExecuteCodeAction(ActionManager.OnPointerOutTrigger, () => {
    hl.removeMesh(mesh)
  }))

  mesh.freezeWorldMatrix()
  mesh.doNotSyncBoundingInfo = true
  scene.blockfreeActiveMeshesAndRenderingGroups = false
}

export { meshBindAction }
